package xyz.vmcserver.wdibbs.model.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import xyz.vmcserver.wdibbs.model.po.ArticleView;
import xyz.vmcserver.wdibbs.model.po.UserView;
import xyz.vmcserver.wdibbs.model.service.ArticleService;
import xyz.vmcserver.wdibbs.model.service.ToolsService;
import xyz.vmcserver.wdibbs.model.service.UserGroupService;
import xyz.vmcserver.wdibbs.model.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
public class IndependenceFunctionController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserGroupService userGroupService;


    @RequestMapping("/sa")
    public ModelAndView search_article(@RequestParam(name = "wd",defaultValue = "error:nullPoint") String text,
                               @RequestParam(name = "p",defaultValue = "1") Integer page,
                               @RequestParam(name = "sv",defaultValue = "create_time") String sortValue,
                               @RequestParam(name = "st",defaultValue = "ASC") String sortType){
        System.out.println("test:==========!"+text+"!"+page+"!"+sortValue+"!"+sortType);
        ModelAndView modelAndView = new ModelAndView();
            List<ArticleView> articleViews;
            articleViews = articleService.searchArticleByKeyAndValuePages(text,page,sortType,sortValue);
            if(0!=articleViews.size()){
                modelAndView.addObject("searchResultArticles",articleViews);
                modelAndView.addObject("searchText",text);
                modelAndView.addObject("searchType","article");
                System.out.println("||num:"+articleViews.size());
            }else{
                modelAndView.addObject("searchResultArticles",null);
                modelAndView.addObject("searchText","快来搜点东西吧~");
                modelAndView.addObject("searchType","article");
            }
        modelAndView.setViewName("module/search_result");
        return modelAndView;
    }
    @RequestMapping("/su")
    public ModelAndView search_user(@RequestParam(name = "wd",defaultValue = "error:nullPoint") String text,
                               @RequestParam(name = "p",defaultValue = "1") Integer page,
                               @RequestParam(name = "sv",defaultValue = "create_time") String sortValue,
                               @RequestParam(name = "st",defaultValue = "ASC") String sortType){
        System.out.println("test:=========="+text+"!"+page+"!"+sortValue+"!"+sortType);
        ModelAndView modelAndView = new ModelAndView();
        List<UserView> userViews = userService.searchUserByKeyAndValuePages(text,page,sortType,sortValue);
        if(0!=userViews.size()){
            System.out.println("userViews"+userViews.toString());
            modelAndView.addObject("searchResultUsers",userViews);
            modelAndView.addObject("searchText",text);
            modelAndView.addObject("searchType","user");
            System.out.println("||num:"+userViews.size());
        }else{
            modelAndView.addObject("searchResultUsers",null);
            modelAndView.addObject("searchText","快来搜点东西吧~");
            modelAndView.addObject("searchType","user");
        }
        modelAndView.setViewName("module/search_result");
        return modelAndView;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Integer upload(@RequestParam(required = false, value = "myFile") String file, HttpServletRequest req) throws IOException {
        if(null==file){
            return -1;
        }
        ToolsService toolsService = new ToolsService();
        String filePath = req.getSession().getServletContext().getRealPath("/userHeader/");
        int result = toolsService.Base64ToFile(file,filePath);
        if(result==1){
            return 1;
        }else{
            return 0;
        }
    }
}
